﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using LHS.Core.TableInfo;
using System.Collections.Specialized;
using System.Collections;
using MS.Core.Constants.Objects;

namespace LHS.Win
{
    public partial class frmXetNghiem : Form
    {
        LHS.Core.Clinical.ClinicalController objController = new LHS.Core.Clinical.ClinicalController();
        // Methods
        public frmXetNghiem()
        {
            this.InitializeComponent();
        }

        private void btClose_Click(object sender, EventArgs e)
        {
            base.Close();
        }

        private void btExit_Click(object sender, EventArgs e)
        {
            base.Close();
        }

        private void btLuu_Click(object sender, EventArgs e)
        {
            if (!this.txtTenTuongUng.Visible)
            {
                if (this.txtXetNghiemMoi.Text == "")
                {
                    MessageBox.Show("Vui lòng nhập tên xét nghiệm mới!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                }
                else
                {
                    this.checkTenXN(this.txtXetNghiemMoi.Text);
                    if (!this.check)
                    {
                        MessageBox.Show("Tên xét nghiệm đã tồn tại!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                    }
                    else
                    {
                        DataTable dt = InfoAnalyzePatient.GetStructure();
                        DataRow dr = dt.NewRow();
                        dr[InfoAnalyzePatient.NLPROOTISN] = 0;
                        dr[InfoAnalyzePatient.NLPNAME] = this.txtXetNghiemMoi.Text.Trim();
                        dt.Rows.Add(dr);
                        try
                        {
                            objController.InsertRow(dr, new string[] { InfoAnalyzePatient.NLPISN });
                            MessageBox.Show("Đã lưu tên xét nghiệm mới!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                        }
                        catch (Exception ex)
                        {
                            Class.ShowException(ex);
                        }
                        
                        this.frmXetNghiem_Load(sender, e);
                        this.txtXoa.Text = "";
                        this.txtOld.Text = "";
                        this.txtTenTuongUng.Text = "";
                        this.txtXetNghiemMoi.Text = "";
                    }
                }
            }
            else if (this.txtXetNghiemMoi.Text == "")
            {
                MessageBox.Show("Vui lòng nhập tên xét nghiệm mới!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
            else
            {
                this.checkTenXN(this.txtXetNghiemMoi.Text);
                if (!this.check)
                {
                    MessageBox.Show("Tên xét nghiệm đã tồn tại!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                }
                else
                {
                    MyNode selectedNode = (MyNode)this.tvBenh.SelectedNode;
                    string str = string.Empty;
                    str = selectedNode.DataRow[0].ToString();
                    try
                    {
                        ListDictionary param = new ListDictionary();
                        param.Add(InfoAnalyzePatient.NLPNAME,this.txtXetNghiemMoi.Text.Trim());
                        param.Add(InfoAnalyzePatient.NLPROOTISN, str);
                        
                        if (this.raCha.Checked)
                        {
                            param.Add("status",0);
                        }
                        if (this.raCon.Checked)
                        {
                              param.Add("status",1);
                        }
                        objController.Exec_StoreProc("sp_AnalyzePatient_Insert",param);
                        MessageBox.Show("Đã lưu tên xét nghiệm mới!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                    }
                    catch (Exception ex)
                    {
                        Class.ShowException(ex);
                    }
                    this.frmXetNghiem_Load(sender, e);
                    this.txtTenTuongUng.Text = "";
                    this.txtXetNghiemMoi.Text = "";
                    this.btLuu.Enabled = false;
                }
            }
        }

        private void btThoat_Click(object sender, EventArgs e)
        {
            base.Close();
        }

        private void btUpdate_Click(object sender, EventArgs e)
        {
            if (this.txtOld.Text == "")
            {
                MessageBox.Show("Vui lòng chọn tên xét nghiệm cần cập nhật!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
            else if (this.txtNew.Text == "")
            {
                MessageBox.Show("Vui lòng nhập tên xét nghiệm mới cần cập nhật!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
            else
            {
                this.checkTenXN(this.txtNew.Text);
                if (!this.check)
                {
                    MessageBox.Show("Tên xét nghiệm đã tồn tại!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                }
                else
                {
                    MyNode selectedNode = (MyNode)this.tvBenh.SelectedNode;
                    string str = string.Empty;
                    str = selectedNode.DataRow[0].ToString();
                    try
                    {
                        ListDictionary param = new ListDictionary();
                        param.Add(InfoAnalyzePatient.NLPNAME, this.txtNew.Text.Trim());
                        param.Add("nlpOldISN", str.Trim());
                        objController.Exec_StoreProc("sp_AnalyzePatient_Update", param);
                    }
                    catch (Exception ex)
                    {
                        Class.ShowException(ex);
                    }
                    MessageBox.Show("Đã cập nhật tên xét nghiệm!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                    this.frmXetNghiem_Load(sender, e);
                    this.txtXoa.Text = "";
                    this.txtNew.Text = "";
                    this.txtOld.Text = "";
                    this.txtTenTuongUng.Text = "";
                    this.btUpdate.Enabled = false;
                }
            }
        }

        private void btXoa_Click(object sender, EventArgs e)
        {
            if (this.txtXoa.Text == "")
            {
                MessageBox.Show("Vui lòng chọn tên xét nghiệm mới cần xóa!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
            else
            {
                MyNode selectedNode = (MyNode)this.tvBenh.SelectedNode;

                string str = string.Empty;
                str = selectedNode.DataRow[0].ToString();

                try
                {
                    ListDictionary param = new ListDictionary();
                    param.Add(InfoAnalyzePatient.NLPISN, str);
                    objController.Exec_StoreProc("sp_AnalyzePatient_Delete", param);
                }
                catch (Exception ex)
                {
                    Class.ShowException(ex);
                }

                MessageBox.Show("Đã xóa tên xét nghiệm!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                this.frmXetNghiem_Load(sender, e);
                this.btXoa.Enabled = false;
                this.txtXoa.Text = "";
                this.txtOld.Text = "";
                this.txtTenTuongUng.Text = "";
            }
        }

        public void checkTenXN(string text)
        {
            this.check = true;

            ListDictionary param = new ListDictionary();
            param.Add(InfoAnalyzePatient.NLPNAME, text);
            object objNLPISN = objController.GetScalar(InfoAnalyzePatient.NLPISN, param, InfoAnalyzePatient.TABLENAME);
            if (objNLPISN is Int64 && Convert.ToInt64(objNLPISN.ToString()) > 0)
                this.check = false;
        }

        private void frmXetNghiem_Load(object sender, EventArgs e)
        {
            this.count++;
            this.tvBenh.Nodes.Clear();
            this.raCha_CheckedChanged(sender, e);

            DataTable dt = objController.GetData(InfoAnalyzePatient.TABLENAME);
            if (dt == null  || dt.Rows.Count == 0)
            {
                this.tbCapNhat.Hide();
                this.tbXoa.Hide();
                this.txtTenTuongUng.Visible = false;
                this.raCha.Visible = false;
                this.raCon.Visible = false;
                this.lbTen.Visible = false;
            }
            else
            {
                this.txtTenTuongUng.Visible = true;
                this.raCha.Visible = true;
                this.raCon.Visible = true;
                this.lbTen.Visible = true;
                this.LoadXetNghiem();
                if (this.count == 1)
                {
                    this.tvBenh.CollapseAll();
                }
            }
        }

        private void insertChildNode(string strName, DataRow row)
        {
            this.node2 = new MyNode(row);
            this.node2.Text = strName;
        }

        private void insertChildNode3(string strName, DataRow row)
        {
            this.node3 = new MyNode(row);
            this.node3.Text = strName;
        }

        private void insertChildNode4(string strName, DataRow row)
        {
            this.node4 = new MyNode(row);
            this.node4.Text = strName;
        }

        private void insertChildNode5(string strName, DataRow row)
        {
            this.node5 = new MyNode(row);
            this.node5.Text = strName;
        }

        private void insertParentNode(string strName, DataRow row)
        {
            this.node = new MyNode(row);
            this.node.Text = strName;
        }

        private void LoadXetNghiem()
        {
            if (arrPltISN != null)
                this.arrPltISN.Clear();
            if (arrPltName != null)
                this.arrPltName.Clear();
            if (arrCha != null)
                this.arrCha.Clear();

            ArrayList arrColumn1 = new ArrayList();
            arrColumn1.Add("nlpISNLevelOne");
            arrColumn1.Add("nlpNameLevelOne");
            List<ExpressionObject> param1 = new List<ExpressionObject>();
            param1.Add(new ExpressionObject("nlpNameLevelOne", "is not null", ExpressionObject.eSelectOperator.None));
            ListDictionary orderby1 = new ListDictionary();
            orderby1.Add("nlpNameLevelOne", "asc");
            DataTable dtAnalyzeTree1 = objController.GetData("vw_AnalyzeTree", true, arrColumn1, param1, orderby1);

            if (dtAnalyzeTree1 != null)
            {
                for (int i = 0; i < dtAnalyzeTree1.Rows.Count; i++)
                {
                    this.arrPltISN.Add(dtAnalyzeTree1.Rows[i][0].ToString());
                    this.arrPltName.Add(dtAnalyzeTree1.Rows[i][1].ToString());
                }
                for (int j = 0; j < this.arrPltISN.Count; j++)
                {
                    this.insertParentNode(this.arrPltName[j].ToString(), dtAnalyzeTree1.Rows[j]);
                    this.tvBenh.Nodes.Add(this.node);

                    ArrayList arrColumn2 = new ArrayList();
                    arrColumn2.Add("nlpISNLevelTwo");
                    arrColumn2.Add("nlpNameLeveltwo");
                    List<ExpressionObject> param2 = new List<ExpressionObject>();
                    param2.Add(new ExpressionObject("nlpNameLeveltwo", "is not null", ExpressionObject.eSelectOperator.None));
                    param2.Add(new ExpressionObject("nlpISNLevelOne", this.arrPltISN[j].ToString()));
                    ListDictionary orderby2 = new ListDictionary();
                    orderby2.Add("nlpNameLevelTwo", "asc");
                    DataTable dtAnalyzeTree2 = objController.GetData("vw_AnalyzeTree", true, arrColumn2, param2, orderby2);

                    for (int k = 0; k < dtAnalyzeTree2.Rows.Count; k++)
                    {
                        this.tvBenh.SelectedNode = this.node;
                        this.insertChildNode(dtAnalyzeTree2.Rows[k]["nlpNameLevelTwo"].ToString(), dtAnalyzeTree2.Rows[k]);
                        this.tvBenh.SelectedNode.Nodes.Add(this.node2);
                        this.arrCha.Add(dtAnalyzeTree2.Rows[k]["nlpNameLevelTwo"].ToString());

                        ArrayList arrColumn3 = new ArrayList();
                        arrColumn3.Add("nlpISNLevelThree");
                        arrColumn3.Add("nlpNameLevelThree");
                        List<ExpressionObject> param3 = new List<ExpressionObject>();
                        param3.Add(new ExpressionObject("nlpNameLevelthree", "is not null", ExpressionObject.eSelectOperator.None));
                        param3.Add(new ExpressionObject("nlpISNLevelTwo", dtAnalyzeTree2.Rows[k]["nlpISNLevelTwo"].ToString()));
                        ListDictionary orderby3 = new ListDictionary();
                        orderby3.Add("nlpNameLevelThree", "asc");
                        DataTable dtAnalyzeTree3 = objController.GetData("vw_AnalyzeTree", true, arrColumn3, param3, orderby3);

                        this.tvBenh.SelectedNode = this.node2;
                        for (int m = 0; m < dtAnalyzeTree3.Rows.Count; m++)
                        {
                            this.arrCha.Remove(k);
                            this.tvBenh.SelectedNode = this.node2;
                            this.insertChildNode3(dtAnalyzeTree3.Rows[m]["nlpNameLevelThree"].ToString(), dtAnalyzeTree3.Rows[m]);
                            this.tvBenh.SelectedNode.Nodes.Add(this.node3);

                            ArrayList arrColumn4 = new ArrayList();
                            arrColumn4.Add("nlpISNLevelFourth");
                            arrColumn4.Add("nlpNameLevelFourth");
                            List<ExpressionObject> param4 = new List<ExpressionObject>();
                            param4.Add(new ExpressionObject("nlpNameLevelFourth", "is not null", ExpressionObject.eSelectOperator.None));
                            param4.Add(new ExpressionObject("nlpISNLevelThree", dtAnalyzeTree3.Rows[m]["nlpISNLevelThree"].ToString()));
                            ListDictionary orderby4 = new ListDictionary();
                            orderby4.Add("nlpNameLevelFourth", "asc");
                            DataTable dtAnalyzeTree4 = objController.GetData("vw_AnalyzeTree", true, arrColumn4, param4, orderby4);

                            this.tvBenh.SelectedNode = this.node3;
                            for (int n = 0; n < dtAnalyzeTree4.Rows.Count; n++)
                            {
                                this.tvBenh.SelectedNode = this.node3;
                                this.insertChildNode4(dtAnalyzeTree4.Rows[n]["nlpNameLevelFourth"].ToString(), dtAnalyzeTree4.Rows[n]);
                                this.tvBenh.SelectedNode.Nodes.Add(this.node4);

                                ArrayList arrColumn5 = new ArrayList();
                                arrColumn5.Add("nlpISNLevelFifth");
                                arrColumn5.Add("nlpNameLevelFifth");
                                List<ExpressionObject> param5 = new List<ExpressionObject>();
                                param5.Add(new ExpressionObject("nlpNameLevelFifth", "is not null", ExpressionObject.eSelectOperator.None));
                                param5.Add(new ExpressionObject("nlpISNLevelFourth", dtAnalyzeTree4.Rows[n]["nlpISNLevelFourth"].ToString()));
                                ListDictionary orderby5 = new ListDictionary();
                                orderby5.Add("nlpNameLevelFifth", "asc");
                                DataTable dtAnalyzeTree5 = objController.GetData("vw_AnalyzeTree", true, arrColumn5, param5, orderby5);

                                this.tvBenh.SelectedNode = this.node4;
                                for (int num6 = 0; num6 < dtAnalyzeTree5.Rows.Count; num6++)
                                {
                                    this.tvBenh.SelectedNode = this.node4;
                                    this.insertChildNode5(dtAnalyzeTree5.Rows[num6]["nlpNameLevelFifth"].ToString(), dtAnalyzeTree5.Rows[num6]);
                                    this.tvBenh.SelectedNode.Nodes.Add(this.node5);
                                }
                            }
                        }
                    }
                }
            }
        }

        private void raCha_CheckedChanged(object sender, EventArgs e)
        {
            if (this.raCha.Checked)
            {
                this.lbTen.Text = "Tên cấp cha :";
            }
        }

        private void raCon_CheckedChanged(object sender, EventArgs e)
        {
            if (this.raCon.Checked)
            {
                this.lbTen.Text = "Tên đồng cấp :";
            }
        }

        private void tvBenh_DoubleClick(object sender, EventArgs e)
        {
            this.btLuu.Enabled = true;
            this.btUpdate.Enabled = true;
            this.btXoa.Enabled = true;
            this.txtTenTuongUng.Text = this.tvBenh.SelectedNode.Text.ToString();
            this.txtOld.Text = this.tvBenh.SelectedNode.Text.ToString();
            this.txtXoa.Text = this.tvBenh.SelectedNode.Text.ToString();
        }

        private void txtTenTuongUng_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = true;
        }

    }
}